/*
 * 2012.2.21 UI개발팀 silion@sk.com
 * 
 * 브라우저, OS 체크, popup(js파일이 인클루드된 html) content wraper dom object의(인자로 지정) offsetWidth,offsetHeight를 읽어와 resize 한다
 * (body의 offset size를 이용하지 않는 이유는 popup을 생성하는 호출 당시 body의 사이즈가 정해지기 때문, window의 size로 정해짐)
 * 
 */
if(!UIHelp) {
	var UIHelp = {};
}

UIHelp.init = function() {
	this._window = window;
	this.getBrowserInfo();
};

UIHelp.getBrowserInfo = function() {
    var browserData = [
        {
            name: 'Explorer',
            parseTarget: navigator.userAgent,
            refer: 'MSIE',
            versionRegExp: /(?:MSIE) ([0-9.]+)/
        },
        {
            name: 'Firefox',
            parseTarget: navigator.userAgent,
            refer: 'Firefox',
            versionRegExp: /(?:Firefox)\/([0-9.]+)/

        },
        {
            name: 'Chrome',
            parseTarget: navigator.userAgent,
            refer: 'Chrome',
            versionRegExp: /(?:Chrome)\/([0-9.]+)/
        },
        {
            name: 'Opera',
            parseTarget: navigator.userAgent,
            refer: 'Opera',
            versionRegExp: /(?:Opera)\/([0-9.]+)/
        },
        {
            name: 'Safari',
            parseTarget: navigator.vendor,
            refer: 'Apple',
            versionRegExp: /(?:Version)\/([0-9.]+)/
        }
    ];
    var osData = [
        {
            name: 'Windows',
            refer: 'Win',
            versionRegExp : /(?:Windows NT) ([0-9.]+)/
        },
        {
            name : 'Mac',
            refer : 'Apple'
        },
        {
            name: 'Linux',
            refer: 'Linux'
        }
    ];    
    var browserCount = browserData.length,
    	osCount = osData.length,
    	currentAgentString,
    	tempObj = {};
    	
    try {
        for (var i = 0; i < browserCount; i++) {
            if(browserData[i].parseTarget.indexOf(browserData[i].refer) != -1) {
            	currentAgentString = browserData[i].parseTarget; 
                tempObj.name    = browserData[i].name;
                tempObj[browserData[i].name.toLowerCase()] = true;
                tempObj.version = navigator.userAgent.match(browserData[i].versionRegExp)[1];
    			break;
            }
        }
        for (var i = 0; i < osCount; i++) {
        	if(currentAgentString.indexOf(osData[i].refer) != -1) {
        		tempObj.osName = osData[i].name;
        		tempObj[osData[i].name.toLowerCase()] = true;
        		if(tempObj.osName == 'Windows') {
        			tempObj.osVersion = currentAgentString.match(osData[i].versionRegExp)[1];
        			switch(tempObj.osVersion) {
        				case "5.1" :
        					tempObj.osSubversion = 'xp';
        					tempObj['xp'] = true;
        					break;
        				case "6.0" :
        					tempObj.osSubversion = 'vista';
        					tempObj['vista'] = true;
        					break;
        				case "6.1" :
        				 	tempObj.osSubversion = 'windows7';
        				 	tempObj['windows7'] = true;
        					break;
        			}
        			break;
        		}
        	}
        }
    } catch(e) {}
    
	if(this!=window && typeof this == 'object') {
	    this.browserInfo = tempObj;
	} else {
		return tempObj;
	}
};
// 초기화 함수이므로 반드시 실행되어야 합니다.
UIHelp.init();

// 팝업 리사이즈 함수 객체
UIHelp.popupResize = function(width,height) {
	if(!UIHelp.browserInfo.explore) {
		var _this = this;
		UIHelp._window.resizeTo(width,height);
		setTimeout(function() {
			_this.resetSize(width,height);
		},50);
	}
}

UIHelp.popupResize.prototype.resetSize = function(width,height) {
	var doc = document, docBody = doc.body;
	var agentInfo = UIHelp.browserInfo;
	var _this = this;
		
	if(agentInfo.explore && docBody.offsetWidth == docBody.scrollWidth) {
		setTimeout(function() {
			_this.resetSize();
		},50);
		return;
	}
	var oHTML = doc.getElementsByTagName('HTML')[0];
	var oTempDiv = docBody.appendChild(doc.createElement('DIV'));
	var scrollCorrection;

	oTempDiv.style.position = 'absolute';
	oTempDiv.style.left = 0;
	oTempDiv.style.top = 0;
	oTempDiv.style.width = '100%';
	oTempDiv.style.height = '100%';
	scrollCorrection = (agentInfo.mac) ? 15 : 16;

	if(agentInfo.chrome || agentInfo.safari) {
		if(!(oHTML.scrollWidth-oTempDiv.clientWidth) || !(oHTML.scrollHeight-oTempDiv.clientHeight)) {
			UIHelp._window.resizeBy(oHTML.scrollWidth-oTempDiv.clientWidth, oHTML.scrollHeight-oTempDiv.clientHeight);
			setTimeout(function() {
				_this.resetSize(width,height);
			},50);
		} else {
			UIHelp._window.resizeBy(oHTML.scrollWidth-oTempDiv.clientWidth-scrollCorrection, oHTML.scrollHeight-oTempDiv.clientHeight-scrollCorrection);
			
			setTimeout(function() {
				var temp = width - oHTML.scrollWidth;
				if(temp>0) {
					UIHelp._window.resizeBy(temp*-1,0);
				} else if(temp<0){
					UIHelp._window.resizeBy(temp,0);
				}
			},50);
		}
	} else {
		if(agentInfo.explorer && parseInt(agentInfo.version)<=7) {
			UIHelp._window.resizeBy(oHTML.scrollWidth-oTempDiv.scrollWidth, docBody.scrollHeight-oHTML.offsetHeight);
		} else {
			UIHelp._window.resizeBy(oHTML.scrollWidth-oTempDiv.scrollWidth, oHTML.scrollHeight-oTempDiv.scrollHeight);
		}
	}
	doc.body.removeChild(oTempDiv);
};

function resize(containerID,defaultWidth,defaultHeight) {
	var container, idType = typeof containerID;
	
	if(!containerID && idType != 'string' && idType != 'object') {
		return false;
	}
	container = (idType == 'object') ? containerID : document.getElementById(containerID);
	
	if(!defaultWidth) {
		defaultWidth = container.offsetWidth
	}
	if(!defaultHeight) {
		defaultHeight = container.offsetHeight;
	}
	
	var temp = new UIHelp.popupResize(defaultWidth,defaultHeight);
};